package com.trkj.usercenter;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.trkj.base.BaseBackFragmentActivity;
import com.trkj.base.Constants;
import com.trkj.base.OnResponseResultListener;
import com.trkj.base.newwork.WaitingDataFromRemote;
import com.trkj.base.utils.UserService;
import com.trkj.taoruiyun.R;

import java.text.MessageFormat;

/**
 * Created by LiZanhong on 2015/06/25.
 */
public class ScoreActivity extends BaseBackFragmentActivity{
    public static final String ACTION = "com.trkj.usercenter.ScoreActivity";
    private Integer userId = -1;
    private String userName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentFragment(new ScoreFragment());
        Intent data = getIntent();
        userId = data.getIntExtra("userId", -1);
        userName = data.getStringExtra("userName");

        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.fragment_attach);
    }


    @Override
    public String getCustomerTitle() {
        return "学分记录";
    }

    @Override
    public String getSubCustomerTitle() {
        return userName == null ? "您的所有学分记录" : userName + "的学分记录";
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        final MenuItem menuItem = menu.add(Menu.NONE, Menu.NONE, Menu.NONE, "loading");
        menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        WaitingDataFromRemote dataFromRemote = new WaitingDataFromRemote(this);
        dataFromRemote.setResponseResultListener(new OnResponseResultListener() {
            @Override
            public void onResponseResult(String result) {
                JSONObject jsonObject = JSON.parseObject(result);
                if(jsonObject.getBoolean("success")){
                    menuItem.setTitle("共" +
                            jsonObject.getJSONObject("data").getString("points")
                            + "分");
                }
            }
        });

        dataFromRemote.execute(MessageFormat.format(Constants.ApiUrl.CHART_SCORE_TOTAL,
                (userId == -1 ? UserService.getInstance(this).parseUser().userId : userId)));


        return super.onCreateOptionsMenu(menu);
    }
}
